/*
 * Copyright 2025, Haiku, Inc. All Rights Reserved.
 * Distributed under the terms of the MIT License.
 *
 * Authors: Amor
 */

/*!	适用于 Haiku 的 Ant Design 5.0 风格装饰器 */

#include "AntDecorator.h"

#include <algorithm>
#include <cmath>
#include <new>
#include <stdio.h>

#include <Autolock.h>
#include <Debug.h>
#include <GradientLinear.h>
#include <Rect.h>
#include <Region.h>
#include <View.h>

#include <WindowPrivate.h>

#include "BitmapDrawingEngine.h"
#include "DesktopSettings.h"
#include "DrawingEngine.h"
#include "DrawState.h"
#include "FontManager.h"
#include "PatternHandler.h"
#include "ServerBitmap.h"

// #define DEBUG_DECORATOR
#ifdef DEBUG_DECORATOR
#define STRACE(x) printf x
#else
#define STRACE(x) ;
#endif

static const float kBorderResizeLength = 22.0;

AntDecorAddOn::AntDecorAddOn(image_id id, const char *name)
	: DecorAddOn(id, name)
{
}

Decorator *
AntDecorAddOn::_AllocateDecorator(DesktopSettings &settings, BRect rect,
								  Desktop *desktop)
{
	return new (std::nothrow) AntDecorator(settings, rect, desktop);
}

static inline uint8
blend_color_value(uint8 a, uint8 b, float position)
{
	int16 delta = (int16)b - a;
	int32 value = a + (int32)(position * delta);
	if (value > 255)
		return 255;
	if (value < 0)
		return 0;

	return (uint8)value;
}

//	#pragma mark -

// TODO：在此处移除 DesktopSettings，并为 Decorator 基类引入私有访问方法
AntDecorator::AntDecorator(DesktopSettings &settings, BRect rect,
						   Desktop *desktop)
	: TabDecorator(settings, rect, desktop)
{
	// TODO：如果装饰器创建时的边框过小，它应该自动调整自己的大小！

	// Initialize Ant Design 5.0 colors
	// Primary color - Ant Design Blue
	fAntPrimaryColor = (rgb_color){24, 144, 255, 255};
	// Success color - Ant Design Green
	fAntSuccessColor = (rgb_color){82, 196, 26, 255};
	// Warning color - Ant Design Gold
	fAntWarningColor = (rgb_color){250, 173, 20, 255};
	// Error color - Ant Design Red
	fAntErrorColor = (rgb_color){245, 34, 45, 255};
	// Info color - Ant Design Cyan
	fAntInfoColor = (rgb_color){19, 194, 194, 255};

	STRACE(("AntDecorator:\n"));
	STRACE(("\tFrame (%.1f,%.1f,%.1f,%.1f)\n",
			rect.left, rect.top, rect.right, rect.bottom));
}

AntDecorator::~AntDecorator()
{
	STRACE(("AntDecorator: ~AntDecorator()\n"));
}

// #pragma mark - Public methods

/*!
	\brief 返回指定装饰器组件的边框颜色。

	颜色数组元素的含义取决于所指定的组件。
	对于某些组件，部分数组元素可能未被使用。

	\param component 要返回边框颜色的组件。
	\param highlight 为该组件设置的高亮状态。
	\param colors 由函数初始化的颜色数组。
*/
void AntDecorator::GetComponentColors(Component component, uint8 highlight,
									  ComponentColors _colors, Decorator::Tab *_tab)
{
	Decorator::Tab *tab = static_cast<Decorator::Tab *>(_tab);
	switch (component)
	{
	case COMPONENT_TAB:
		if (highlight != 0)
		{
			_colors[COLOR_TAB_FRAME_LIGHT] = tint_color(fAntPrimaryColor, 1.0);
			_colors[COLOR_TAB_FRAME_DARK] = tint_color(fAntPrimaryColor, 1.2);
			_colors[COLOR_TAB] = tint_color(fAntPrimaryColor, 0.95);
			_colors[COLOR_TAB_LIGHT] = tint_color(fAntPrimaryColor, 0.8);
			_colors[COLOR_TAB_BEVEL] = tint_color(fAntPrimaryColor, 0.7);
			_colors[COLOR_TAB_SHADOW] = tint_color(fAntPrimaryColor, 1.3);
			_colors[COLOR_TAB_TEXT] = (rgb_color){255, 255, 255, 255};
		}
		else if (tab && tab->buttonFocus)
		{
			_colors[COLOR_TAB_FRAME_LIGHT] = tint_color(fAntPrimaryColor, 1.0);
			_colors[COLOR_TAB_FRAME_DARK] = tint_color(fAntPrimaryColor, 1.2);
			_colors[COLOR_TAB] = fAntPrimaryColor;
			_colors[COLOR_TAB_LIGHT] = tint_color(fAntPrimaryColor, 0.8);
			_colors[COLOR_TAB_BEVEL] = tint_color(fAntPrimaryColor, 0.7);
			_colors[COLOR_TAB_SHADOW] = tint_color(fAntPrimaryColor, 1.3);
			_colors[COLOR_TAB_TEXT] = (rgb_color){255, 255, 255, 255};
		}
		else
		{
			// Non-focused tab uses a more neutral color
			rgb_color nonFocusColor = (rgb_color){240, 240, 240, 255};
			_colors[COLOR_TAB_FRAME_LIGHT] = tint_color(nonFocusColor, 1.0);
			_colors[COLOR_TAB_FRAME_DARK] = tint_color(nonFocusColor, 1.2);
			_colors[COLOR_TAB] = nonFocusColor;
			_colors[COLOR_TAB_LIGHT] = tint_color(nonFocusColor, 0.8);
			_colors[COLOR_TAB_BEVEL] = tint_color(nonFocusColor, 0.7);
			_colors[COLOR_TAB_SHADOW] = tint_color(nonFocusColor, 1.3);
			_colors[COLOR_TAB_TEXT] = (rgb_color){0, 0, 0, 255};
		}
		break;

	case COMPONENT_CLOSE_BUTTON:
		if (tab && tab->buttonFocus)
		{
			_colors[COLOR_BUTTON] = fAntErrorColor;
			_colors[COLOR_BUTTON_LIGHT] = tint_color(fAntErrorColor, 0.8);
		}
		else
		{
			rgb_color nonFocusColor = (rgb_color){240, 240, 240, 255};
			_colors[COLOR_BUTTON] = nonFocusColor;
			_colors[COLOR_BUTTON_LIGHT] = tint_color(nonFocusColor, 0.8);
		}
		break;

	case COMPONENT_ZOOM_BUTTON:
		if (tab && tab->buttonFocus)
		{
			_colors[COLOR_BUTTON] = fAntSuccessColor;
			_colors[COLOR_BUTTON_LIGHT] = tint_color(fAntSuccessColor, 0.8);
		}
		else
		{
			rgb_color nonFocusColor = (rgb_color){240, 240, 240, 255};
			_colors[COLOR_BUTTON] = nonFocusColor;
			_colors[COLOR_BUTTON_LIGHT] = tint_color(nonFocusColor, 0.8);
		}
		break;

	case COMPONENT_TOP_BORDER:
		if (tab && tab->buttonFocus)
		{
			_colors[0] = tint_color(fAntPrimaryColor, 1.2); // outer border
			_colors[1] = tint_color(fAntPrimaryColor, 1.0); // top border
			_colors[2] = tint_color(fAntPrimaryColor, 1.0); // top border
			_colors[3] = tint_color(fAntPrimaryColor, 1.0); // top border
			_colors[4] = tint_color(fAntPrimaryColor, 0.9); // inner border
			_colors[5] = tint_color(fAntPrimaryColor, 0.9); // menu border
		}
		else
		{
			rgb_color nonFocusColor = (rgb_color){240, 240, 240, 255};
			_colors[0] = tint_color(nonFocusColor, 1.2); // outer border
			_colors[1] = tint_color(nonFocusColor, B_NO_TINT);
			_colors[2] = tint_color(nonFocusColor, B_NO_TINT);
			_colors[3] = tint_color(nonFocusColor, B_NO_TINT);
			_colors[4] = tint_color(nonFocusColor, 0.9); // inner border
			_colors[5] = tint_color(nonFocusColor, 0.9); // menu border
		}
		break;

	case COMPONENT_RESIZE_CORNER:
		if (tab && tab->buttonFocus)
		{
			_colors[0] = tint_color(fAntPrimaryColor, 1.25); // outer border
			_colors[1] = tint_color(fAntPrimaryColor, 1.0);	 // top border
			_colors[2] = tint_color(fAntPrimaryColor, 1.0);	 // top border
			_colors[3] = tint_color(fAntPrimaryColor, 1.0);	 // top border
			_colors[4] = tint_color(fAntPrimaryColor, 0.9);	 // inner border
			_colors[5] = tint_color(fAntPrimaryColor, 0.9);	 // menu border
		}
		else
		{
			rgb_color nonFocusColor = (rgb_color){240, 240, 240, 255};
			_colors[0] = tint_color(nonFocusColor, 1.25); // outer border
			_colors[1] = tint_color(nonFocusColor, B_NO_TINT);
			_colors[2] = tint_color(nonFocusColor, B_NO_TINT);
			_colors[3] = tint_color(nonFocusColor, B_NO_TINT);
			_colors[4] = tint_color(nonFocusColor, 0.9); // inner border
			_colors[5] = tint_color(nonFocusColor, 0.9); // menu border
		}
		break;

	case COMPONENT_LEFT_BORDER:
	case COMPONENT_RIGHT_BORDER:
		if (tab && tab->buttonFocus)
		{
			_colors[0] = tint_color(fAntPrimaryColor, 1.25); // outer border
			_colors[1] = tint_color(fAntPrimaryColor, B_NO_TINT);
			_colors[2] = tint_color(fAntPrimaryColor, B_NO_TINT);
			_colors[3] = tint_color(fAntPrimaryColor, B_NO_TINT);
			_colors[4] = tint_color(fAntPrimaryColor, 0.9); // inner border
			_colors[5] = tint_color(fAntPrimaryColor, 0.9); // menu border
			_colors[6] = tint_color(fAntPrimaryColor, 1.2); // tab border part
		}
		else
		{
			rgb_color nonFocusColor = (rgb_color){240, 240, 240, 255};
			_colors[0] = tint_color(nonFocusColor, 1.25); // outer border
			_colors[1] = tint_color(nonFocusColor, B_NO_TINT);
			_colors[2] = tint_color(nonFocusColor, B_NO_TINT);
			_colors[3] = tint_color(nonFocusColor, B_NO_TINT);
			_colors[4] = tint_color(nonFocusColor, 0.9); // inner border
			_colors[5] = tint_color(nonFocusColor, 0.9); // menu border
			_colors[6] = tint_color(nonFocusColor, 1.2); // tab border part
		}
		break;

	case COMPONENT_BOTTOM_BORDER:
	default:
		if (tab && tab->buttonFocus)
		{
			_colors[0] = tint_color(fAntPrimaryColor, 1.25); // outer border
			_colors[1] = tint_color(fAntPrimaryColor, B_NO_TINT);
			_colors[2] = tint_color(fAntPrimaryColor, B_NO_TINT);
			_colors[3] = tint_color(fAntPrimaryColor, B_NO_TINT);
			_colors[4] = tint_color(fAntPrimaryColor, 0.9); // inner border
			_colors[5] = tint_color(fAntPrimaryColor, 0.9); // menu border
		}
		else
		{
			rgb_color nonFocusColor = (rgb_color){240, 240, 240, 255};
			_colors[0] = tint_color(nonFocusColor, 1.25); // outer border
			_colors[1] = tint_color(nonFocusColor, B_NO_TINT);
			_colors[2] = tint_color(nonFocusColor, B_NO_TINT);
			_colors[3] = tint_color(nonFocusColor, B_NO_TINT);
			_colors[4] = tint_color(nonFocusColor, 0.9); // inner border
			_colors[5] = tint_color(nonFocusColor, 0.9); // menu border
		}

		// for the resize-border highlight dye everything bluish.
		if (highlight == HIGHLIGHT_RESIZE_BORDER)
		{
			for (int32 i = 0; i < 6; i++)
			{
				_colors[i].red = std::max((int)_colors[i].red - 80, 0);
				_colors[i].green = std::max((int)_colors[i].green - 80, 0);
				_colors[i].blue = 255;
			}
		}
		break;
	}
}

void AntDecorator::UpdateColors(DesktopSettings &settings)
{
	TabDecorator::UpdateColors(settings);

	// 根据系统设置更新 Ant Design 的颜色
	// 这使得装饰器能够在适应系统主题变化的同时，
	// 保持 Ant Design 的配色方案。

	// Primary color - Ant Design Blue
	fAntPrimaryColor = (rgb_color){24, 144, 255, 255};
	// Success color - Ant Design Green
	fAntSuccessColor = (rgb_color){82, 196, 26, 255};
	// Warning color - Ant Design Gold
	fAntWarningColor = (rgb_color){250, 173, 20, 255};
	// Error color - Ant Design Red
	fAntErrorColor = (rgb_color){245, 34, 45, 255};
	// Info color - Ant Design Cyan
	fAntInfoColor = (rgb_color){19, 194, 194, 255};
}

// #pragma mark - Protected methods

void AntDecorator::_DrawFrame(BRect rect)
{
	STRACE(("_DrawFrame(%f,%f,%f,%f)\n", rect.left, rect.top,
			rect.right, rect.bottom));

	// 注意：为了确保裁剪区域在此装饰器中保持有效，
	// DrawingEngine 需要在整个过程中保持锁定状态。

	if (fTopTab->look == B_NO_BORDER_WINDOW_LOOK)
		return;

	if (fBorderWidth <= 0)
		return;

	// 使用 Ant Design 风格绘制边框框架
	BRect r = BRect(fTopBorder.LeftTop(), fBottomBorder.RightBottom());
	switch ((int)fTopTab->look)
	{
	case B_TITLED_WINDOW_LOOK:
	case B_DOCUMENT_WINDOW_LOOK:
	case B_MODAL_WINDOW_LOOK:
	{
		// left border
		if (rect.Intersects(fLeftBorder.InsetByCopy(0, -fBorderWidth)))
		{
			ComponentColors colors;
			_GetComponentColors(COMPONENT_LEFT_BORDER, colors, fTopTab);

			// Ant Design uses subtle single line border
			fDrawingEngine->StrokeLine(BPoint(r.left, r.top),
									   BPoint(r.left, r.bottom), colors[0]);

			// redraw line to be part of tab title
			fDrawingEngine->StrokeLine(BPoint(r.left, r.top),
									   BPoint(r.left, r.top + 4), colors[6]);
		}

		// bottom border
		if (rect.Intersects(fBottomBorder))
		{
			ComponentColors colors;
			_GetComponentColors(COMPONENT_BOTTOM_BORDER, colors, fTopTab);

			// Single subtle border line
			fDrawingEngine->StrokeLine(BPoint(r.left, r.bottom),
									   BPoint(r.right, r.bottom), colors[0]);
		}

		// right border
		if (rect.Intersects(fRightBorder.InsetByCopy(0, -fBorderWidth)))
		{
			ComponentColors colors;
			_GetComponentColors(COMPONENT_RIGHT_BORDER, colors, fTopTab);

			// Single subtle border line
			fDrawingEngine->StrokeLine(BPoint(r.right, r.top),
									   BPoint(r.right, r.bottom), colors[0]);

			// 重新绘制线条以作为标签标题的一部分
			fDrawingEngine->StrokeLine(BPoint(r.right, r.top),
									   BPoint(r.right, r.top + 4), colors[6]);
		}

		// top border
		if (rect.Intersects(fTopBorder))
		{
			ComponentColors colors;
			_GetComponentColors(COMPONENT_TOP_BORDER, colors, fTopTab);

			// Ant Design 使用微妙的单线边框
			fDrawingEngine->StrokeLine(BPoint(r.left, r.top),
									   BPoint(r.right, r.top), colors[0]);
		}
		break;
	}

	case B_FLOATING_WINDOW_LOOK:
	case kLeftTitledWindowLook:
	{
		// 类似于带标题的窗口，但左边框更细。
		if (rect.Intersects(fLeftBorder.InsetByCopy(0, -fBorderWidth)))
		{
			ComponentColors colors;
			_GetComponentColors(COMPONENT_LEFT_BORDER, colors, fTopTab);
			fDrawingEngine->StrokeLine(BPoint(r.left, r.top),
									   BPoint(r.left, r.bottom), colors[0]);
		}

		// bottom border
		if (rect.Intersects(fBottomBorder))
		{
			ComponentColors colors;
			_GetComponentColors(COMPONENT_BOTTOM_BORDER, colors, fTopTab);
			fDrawingEngine->StrokeLine(BPoint(r.left, r.bottom),
									   BPoint(r.right, r.bottom), colors[0]);
		}

		// right border
		if (rect.Intersects(fRightBorder.InsetByCopy(0, -fBorderWidth)))
		{
			ComponentColors colors;
			_GetComponentColors(COMPONENT_RIGHT_BORDER, colors, fTopTab);
			fDrawingEngine->StrokeLine(BPoint(r.right, r.top),
									   BPoint(r.right, r.bottom), colors[0]);
		}

		// top border
		if (rect.Intersects(fTopBorder))
		{
			ComponentColors colors;
			_GetComponentColors(COMPONENT_TOP_BORDER, colors, fTopTab);
			fDrawingEngine->StrokeLine(BPoint(r.left, r.top),
									   BPoint(r.right, r.top), colors[0]);
		}
		break;
	}

	case B_BORDERED_WINDOW_LOOK:
	{
		// 带边框窗口的简单单一边框
		ComponentColors colors;
		_GetComponentColors(COMPONENT_LEFT_BORDER, colors, fTopTab);
		fDrawingEngine->StrokeRect(r, colors[0]);
		break;
	}

	default:
		// 不绘制边框框架
		break;
	}

	// 如果需要，绘制调整大小的角落
	if (!(fTopTab->flags & B_NOT_RESIZABLE))
	{
		r = fResizeRect;
		if (rect.Intersects(r))
		{
			ComponentColors colors;
			_GetComponentColors(COMPONENT_RESIZE_CORNER, colors, fTopTab);

			// 调整大小角落的简单对角线
			fDrawingEngine->StrokeLine(BPoint(r.right - 3, r.bottom),
									   BPoint(r.right, r.bottom - 3), colors[0]);
			fDrawingEngine->StrokeLine(BPoint(r.right - 6, r.bottom),
									   BPoint(r.right, r.bottom - 6), colors[0]);
			fDrawingEngine->StrokeLine(BPoint(r.right - 9, r.bottom),
									   BPoint(r.right, r.bottom - 9), colors[0]);
		}
	}
}

/*!
 *	\brief 实际绘制标题
 *
 *	此函数的主要任务是确保装饰器仅在自己的区域内绘制标题，并完成标题本身的绘制。
 *	建议使用 B_OP_COPY 来绘制标题，因为其他绘图模式可能会显著影响性能，但这并不是强制要求。
 *
 *	\param tab 要更新的 \a tab。
 *	\param invalid 无效区域。
 */
void AntDecorator::_DrawTab(Decorator::Tab *tab, BRect invalid)
{
	STRACE(("_DrawTab(%.1f,%.1f,%.1f,%.1f)\n",
			invalid.left, invalid.top, invalid.right, invalid.bottom));

	const BRect &tabRect = tab->tabRect;
	if (!tabRect.IsValid() || !invalid.Intersects(tabRect))
		return;

	ComponentColors colors;
	_GetComponentColors(COMPONENT_TAB, colors, tab);

	// 绘制标签背景 - 使用Ant Design风格的渐变填充
	BGradientLinear gradient;
	gradient.SetStart(tabRect.LeftTop());

	if (tab && tab->buttonFocus)
	{
		// 焦点状态使用更深的渐变
		gradient.AddColor(tint_color(colors[COLOR_TAB], 0.8), 0);
		gradient.AddColor(colors[COLOR_TAB], 200);
	}
	else
	{
		// 普通状态使用轻微渐变
		gradient.AddColor(tint_color(colors[COLOR_TAB], 0.95), 0);
		gradient.AddColor(colors[COLOR_TAB], 150);
	}

	// 根据标签位置设置渐变方向
	if (tab->look != kLeftTitledWindowLook)
	{
		gradient.SetEnd(tabRect.LeftBottom());
		fDrawingEngine->FillRect(BRect(tabRect.left + 1, tabRect.top + 1,
									   tabRect.right - 1, tabRect.bottom - 1),
								 gradient);
	}
	else
	{
		gradient.SetEnd(tabRect.RightTop());
		fDrawingEngine->FillRect(BRect(tabRect.left + 1, tabRect.top + 1,
									   tabRect.right - 1, tabRect.bottom - 1),
								 gradient);
	}

	// 绘制标签边框 - Ant Design风格使用更细的边框
	fDrawingEngine->StrokeLine(tabRect.LeftTop(), tabRect.LeftBottom(),
							   colors[COLOR_TAB_FRAME_DARK]);
	fDrawingEngine->StrokeLine(tabRect.LeftTop(), tabRect.RightTop(),
							   colors[COLOR_TAB_FRAME_DARK]);

	if (tab->look != kLeftTitledWindowLook)
	{
		fDrawingEngine->StrokeLine(tabRect.RightTop(), tabRect.RightBottom(),
								   colors[COLOR_TAB_FRAME_DARK]);
	}
	else
	{
		fDrawingEngine->StrokeLine(tabRect.LeftBottom(), tabRect.RightBottom(),
								   colors[COLOR_TAB_FRAME_DARK]);
	}

	// 绘制顶部高光线 - Ant Design风格更柔和
	fDrawingEngine->StrokeLine(BPoint(tabRect.left + 1, tabRect.top + 1),
							   BPoint(tabRect.right - 1, tabRect.top + 1), colors[COLOR_TAB_LIGHT]);

	_DrawTitle(tab, tabRect);
	_DrawButtons(tab, invalid);
}

/*!
 *	\brief 实际绘制标题
 *
 *	此函数的主要任务是确保装饰器仅在自己的区域内绘制标题，并完成标题本身的绘制。
 *	建议使用 B_OP_COPY 来绘制标题，因为其他绘图模式可能会显著影响性能，但这并不是强制要求。
 *
 *	\param tab 要更新的 \a tab。
 *	\param rect 标题需要更新的区域。
 */
void AntDecorator::_DrawTitle(Decorator::Tab *_tab, BRect r)
{
	STRACE(("_DrawTitle(%.1f,%.1f,%.1f,%.1f)\n", r.left, r.top, r.right, r.bottom));

	Decorator::Tab *tab = static_cast<Decorator::Tab *>(_tab);

	const BRect &tabRect = tab->tabRect;
	const BRect &closeRect = tab->closeRect;
	const BRect &zoomRect = tab->zoomRect;

	ComponentColors colors;
	_GetComponentColors(COMPONENT_TAB, colors, tab);

	fDrawingEngine->SetDrawingMode(B_OP_OVER);
	fDrawingEngine->SetHighColor(colors[COLOR_TAB_TEXT]);
	fDrawingEngine->SetFont(fDrawState.Font());

	// 确定文本的位置
	font_height fontHeight;
	fDrawState.Font().GetHeight(fontHeight);

	BPoint titlePos;
	// titlePos.x = r.left + tab->textOffset;
	// titlePos.y = floorf(r.top + ((r.Height() - (fontHeight.ascent + fontHeight.descent)) / 2) + fontHeight.ascent + 0.5);
	if (tab->look != kLeftTitledWindowLook)
	{
		titlePos.x = closeRect.IsValid() ? closeRect.right + tab->textOffset
										 : tabRect.left + tab->textOffset;
		titlePos.y = floorf(((tabRect.top + 2.0) + tabRect.bottom + fontHeight.ascent + fontHeight.descent) / 2.0 - fontHeight.descent + 0.5);
	}
	else
	{
		titlePos.x = floorf(((tabRect.left + 2.0) + tabRect.right + fontHeight.ascent + fontHeight.descent) / 2.0 - fontHeight.descent + 0.5);
		titlePos.y = zoomRect.IsValid() ? zoomRect.top - tab->textOffset
										: tabRect.bottom - tab->textOffset;
	}

	// Ant Design 风格 - 简洁、现代的排版
	fDrawingEngine->SetHighColor(colors[COLOR_TAB_TEXT]);
	fDrawingEngine->SetLowColor(colors[COLOR_TAB]);
	fDrawingEngine->DrawString(tab->truncatedTitle, tab->truncatedTitleLength,
							   titlePos);

	fDrawingEngine->SetDrawingMode(B_OP_COPY);
}

void AntDecorator::_DrawClose(Decorator::Tab *tab, bool direct, BRect rect)
{
	STRACE(("_DrawClose(%s, direct=%s, rect=(%.1f,%.1f,%.1f,%.1f))\n",
			tab->name, direct ? "true" : "false", rect.left, rect.top, rect.right,
			rect.bottom));

	if (tab->look == B_NO_BORDER_WINDOW_LOOK)
		return;

	// Ant Design style close button - clean X symbol
	ComponentColors colors;
	_GetComponentColors(COMPONENT_CLOSE_BUTTON, colors, tab);

	// Draw button background - circular for Ant Design style
	BRect buttonRect = rect.InsetByCopy(1, 1);
	if (tab->buttonFocus)
	{
		// Use error color for close button when focused
		BGradient gradient;
		gradient.AddColor(fAntErrorColor, 0);
		gradient.AddColor(tint_color(fAntErrorColor, 0.8), 255);
		fDrawingEngine->FillEllipse(buttonRect, gradient);
		rgb_color white = {255, 255, 255, 255};
		fDrawingEngine->SetHighColor(white); // White X
	}
	else
	{
		// Use neutral color when not focused
		fDrawingEngine->SetHighColor(colors[COLOR_BUTTON]);
		BGradient gradient;
		gradient.AddColor(fAntErrorColor, 0);
		gradient.AddColor(tint_color(fAntErrorColor, 0.8), 255);
		fDrawingEngine->FillEllipse(buttonRect.InsetByCopy(1, 1), gradient);
		rgb_color gray = {100, 100, 100, 255};
		fDrawingEngine->SetHighColor(gray); // Dark gray X
	}

	// Draw X symbol
	float inset = 3.0f;
	fDrawingEngine->StrokeLine(
		BPoint(rect.left + inset, rect.top + inset),
		BPoint(rect.right - inset, rect.bottom - inset));
	fDrawingEngine->StrokeLine(
		BPoint(rect.right - inset, rect.top + inset),
		BPoint(rect.left + inset, rect.bottom - inset));
}

void AntDecorator::_DrawZoom(Decorator::Tab *tab, bool direct, BRect rect)
{
	STRACE(("_DrawZoom(%s, direct=%s, rect=(%.1f,%.1f,%.1f,%.1f))\n",
			tab->name, direct ? "true" : "false", rect.left, rect.top, rect.right,
			rect.bottom));

	if (tab->look == B_NO_BORDER_WINDOW_LOOK)
		return;

	// Ant Design 风格的缩放按钮 - 最大化符号
	ComponentColors colors;
	_GetComponentColors(COMPONENT_ZOOM_BUTTON, colors, tab);

	// 绘制按钮背景 - Ant Design 风格为圆形
	BRect buttonRect = rect.InsetByCopy(1, 1);
	if (tab->buttonFocus)
	{
		// Use success color for zoom button when focused
		fDrawingEngine->SetHighColor(fAntSuccessColor);
		BGradient gradient;
		gradient.AddColor(fAntErrorColor, 0);
		gradient.AddColor(tint_color(fAntErrorColor, 0.8), 255);
		fDrawingEngine->FillEllipse(buttonRect, gradient);
		rgb_color white = {255, 255, 255, 255};
		fDrawingEngine->SetHighColor(white); // White symbol
	}
	else
	{
		// Use neutral color when not focused
		fDrawingEngine->SetHighColor(colors[COLOR_BUTTON]);
		BGradient gradient;
		gradient.AddColor(fAntErrorColor, 0);
		gradient.AddColor(tint_color(fAntErrorColor, 0.8), 255);
		fDrawingEngine->FillEllipse(buttonRect.InsetByCopy(1, 1), gradient);
		rgb_color gray = {100, 100, 100, 255};
		fDrawingEngine->SetHighColor(gray); // Dark gray symbol
	}

	// Draw maximize symbol (square)
	float inset = 4.0f;
	BRect zoomRect(rect.left + inset, rect.top + inset,
				   rect.right - inset, rect.bottom - inset);
	fDrawingEngine->StrokeRect(zoomRect);
}

void AntDecorator::_DrawMinimize(Decorator::Tab *tab, bool direct, BRect rect)
{
	STRACE(("_DrawMinimize(%s, direct=%s, rect=(%.1f,%.1f,%.1f,%.1f))\n",
			tab->name, direct ? "true" : "false", rect.left, rect.top, rect.right,
			rect.bottom));

	if (tab->look == B_NO_BORDER_WINDOW_LOOK)
		return;

	// Ant Design 风格的最小化按钮 - 水平线
	ComponentColors colors;
	_GetComponentColors(COMPONENT_TAB, colors, tab);

	// 绘制按钮背景 - Ant Design 风格为圆形
	BRect buttonRect = rect.InsetByCopy(1, 1);
	if (tab->buttonFocus)
	{
		// 当最小化按钮获得焦点时，使用警告颜色绘制
		fDrawingEngine->SetHighColor(fAntWarningColor);
		BGradient gradient;
		gradient.AddColor(fAntErrorColor, 0);
		gradient.AddColor(tint_color(fAntErrorColor, 0.8), 255);
		fDrawingEngine->FillEllipse(buttonRect.InsetByCopy(1, 1), gradient);
		rgb_color white = {255, 255, 255, 255};
		fDrawingEngine->SetHighColor(white); // White symbol
	}
	else
	{
		// 未获得焦点时使用中性颜色绘制
		fDrawingEngine->SetHighColor(colors[COLOR_TAB]);
		BGradient gradient;
		gradient.AddColor(fAntErrorColor, 0);
		gradient.AddColor(tint_color(fAntErrorColor, 0.8), 255);
		fDrawingEngine->FillEllipse(buttonRect.InsetByCopy(1, 1), gradient);
		rgb_color gray = {100, 100, 100, 255};
		fDrawingEngine->SetHighColor(gray); // 深灰色符号
	}

	// 绘制最小化符号（水平线）
	float inset = 4.0f;
	fDrawingEngine->StrokeLine(
		BPoint(rect.left + inset, rect.top + rect.Height() / 2),
		BPoint(rect.right - inset, rect.top + rect.Height() / 2));
}

// #pragma mark - 私有方法

void AntDecorator::_DrawButtonBitmap(ServerBitmap *bitmap, bool direct, BRect rect)
{
	if (bitmap == NULL)
		return;

	// 保存当前的前台绘制状态
	bool copyToFrontEnabled = fDrawingEngine->CopyToFrontEnabled();

	// 根据direct参数设置是否直接绘制到前台
	fDrawingEngine->SetCopyToFrontEnabled(direct);

	// 保存当前绘制模式并设置为B_OP_OVER
	drawing_mode oldMode;
	fDrawingEngine->SetDrawingMode(B_OP_OVER, oldMode);

	// 绘制位图，使用Ant Design风格的平滑绘制
	fDrawingEngine->DrawBitmap(bitmap, rect.OffsetToCopy(0, 0), rect);

	// 恢复之前的绘制模式
	fDrawingEngine->SetDrawingMode(oldMode);

	// 恢复前台绘制状态
	fDrawingEngine->SetCopyToFrontEnabled(copyToFrontEnabled);
}

/*!
 * \brief 绘制一个带有渐变效果的带边框矩形。
 * \param engine 使用的绘图引擎。
 * \param rect 要绘制的矩形区域。
 * \param down 矩形是否应凹陷绘制。
 * \param colors 包含要使用的颜色的按钮颜色数组。
 */
void AntDecorator::_DrawBlendedRect(DrawingEngine *engine, const BRect rect,
									bool down, const ComponentColors &colors)
{
	engine->FillRect(rect, B_TRANSPARENT_COLOR);

	// Ant Design风格使用扁平颜色和微妙渐变
	rgb_color startColor, endColor;
	if (down)
	{
		// 按下状态使用稍深的颜色
		startColor = tint_color(colors[0], 1.1);
		endColor = tint_color(colors[0], 0.95);
	}
	else
	{
		// 正常状态使用轻微渐变
		startColor = tint_color(colors[0], 0.98);
		endColor = tint_color(colors[0], 0.92);
	}

	// 创建渐变填充
	BGradientLinear gradient;
	gradient.SetStart(rect.LeftTop());
	gradient.SetEnd(rect.RightBottom());
	gradient.AddColor(startColor, 0);
	gradient.AddColor(endColor, 255);

	// 填充矩形
	engine->FillRect(rect, gradient);

	// 添加微妙的边框
	engine->StrokeRect(rect, tint_color(colors[0], 1.15));
}

ServerBitmap *
AntDecorator::_GetBitmapForButton(Decorator::Tab *tab, Component item,
								  bool down, int32 width, int32 height)
{
	// 使用共享位图列表来缓存按钮位图
	// TODO：共享位图列表从未被释放。
	struct decorator_bitmap
	{
		Component item;
		bool down;
		int32 width;
		int32 height;
		rgb_color baseColor;
		rgb_color lightColor;
		UtilityBitmap *bitmap;
		decorator_bitmap *next;
	};

	static BLocker sBitmapListLock("ant decorator lock", true);
	static decorator_bitmap *sBitmapList = NULL;

	ComponentColors colors;
	_GetComponentColors(item, colors, tab);

	BAutolock locker(sBitmapListLock);

	// 在列表中查找匹配的位图
	// TODO：是否应该使用哈希表代替？
	decorator_bitmap *current = sBitmapList;
	while (current)
	{
		if (current->item == item && current->down == down && current->width == width && current->height == height && current->baseColor == colors[COLOR_BUTTON] && current->lightColor == colors[COLOR_BUTTON_LIGHT])
		{
			return current->bitmap;
		}
		current = current->next;
	}

	static BitmapDrawingEngine *sBitmapDrawingEngine = NULL;

	// 创建新的位图
	if (sBitmapDrawingEngine == NULL)
		sBitmapDrawingEngine = new (std::nothrow) BitmapDrawingEngine();
	if (sBitmapDrawingEngine == NULL || sBitmapDrawingEngine->SetSize(width, height) != B_OK)
		return NULL;

	BRect rect(0, 0, width - 1, height - 1);

	// 根据Ant Design风格绘制按钮
	switch (item)
	{
	case COMPONENT_CLOSE_BUTTON:
	{
		// 绘制圆形背景
		BRect buttonRect = rect.InsetByCopy(1, 1);
		BGradientLinear gradient;
		gradient.AddColor(colors[COLOR_BUTTON], 0);
		gradient.AddColor(tint_color(colors[COLOR_BUTTON], 0.8), 255);
		sBitmapDrawingEngine->FillEllipse(buttonRect, gradient);

		// 绘制X符号
		float inset = 3.0f;
		sBitmapDrawingEngine->StrokeLine(
			BPoint(rect.left + inset, rect.top + inset),
			BPoint(rect.right - inset, rect.bottom - inset),
			down ? tint_color(colors[COLOR_BUTTON_LIGHT], 1.2) : colors[COLOR_BUTTON_LIGHT]);
		sBitmapDrawingEngine->StrokeLine(
			BPoint(rect.right - inset, rect.top + inset),
			BPoint(rect.left + inset, rect.bottom - inset),
			down ? tint_color(colors[COLOR_BUTTON_LIGHT], 1.2) : colors[COLOR_BUTTON_LIGHT]);
		break;
	}

	case COMPONENT_ZOOM_BUTTON:
	{
		// 绘制圆形背景
		BRect buttonRect = rect.InsetByCopy(1, 1);
		BGradientLinear gradient;
		gradient.AddColor(colors[COLOR_BUTTON], 0);
		gradient.AddColor(tint_color(colors[COLOR_BUTTON], 0.8), 255);
		sBitmapDrawingEngine->FillEllipse(buttonRect, gradient);

		// 绘制最大化符号(方形)
		float inset = 4.0f;
		BRect zoomRect(rect.left + inset, rect.top + inset,
					   rect.right - inset, rect.bottom - inset);
		sBitmapDrawingEngine->StrokeRect(zoomRect,
										 down ? tint_color(colors[COLOR_BUTTON_LIGHT], 1.2) : colors[COLOR_BUTTON_LIGHT]);
		break;
	}

	default:
		break;
	}

	UtilityBitmap *bitmap = sBitmapDrawingEngine->ExportToBitmap(width, height,
																 B_RGB32);
	if (bitmap == NULL)
		return NULL;

	// 将位图添加到缓存列表
	decorator_bitmap *entry = new (std::nothrow) decorator_bitmap;
	if (entry == NULL)
	{
		delete bitmap;
		return NULL;
	}

	entry->item = item;
	entry->down = down;
	entry->width = width;
	entry->height = height;
	entry->bitmap = bitmap;
	entry->baseColor = colors[COLOR_BUTTON];
	entry->lightColor = colors[COLOR_BUTTON_LIGHT];
	entry->next = sBitmapList;
	sBitmapList = entry;

	return bitmap;
}

void AntDecorator::_GetComponentColors(Component component, ComponentColors _colors,
									   Decorator::Tab *tab)
{
	// 根据组件类型确定高亮区域
	Region region = REGION_NONE;
	switch (component)
	{
	case COMPONENT_TAB:
		region = REGION_TAB;
		break;
	case COMPONENT_CLOSE_BUTTON:
		region = REGION_CLOSE_BUTTON;
		break;
	case COMPONENT_ZOOM_BUTTON:
		region = REGION_ZOOM_BUTTON;
		break;
	case COMPONENT_LEFT_BORDER:
		region = REGION_LEFT_BORDER;
		break;
	case COMPONENT_RIGHT_BORDER:
		region = REGION_RIGHT_BORDER;
		break;
	case COMPONENT_TOP_BORDER:
		region = REGION_TOP_BORDER;
		break;
	case COMPONENT_BOTTOM_BORDER:
		region = REGION_BOTTOM_BORDER;
		break;
	case COMPONENT_RESIZE_CORNER:
		region = REGION_RIGHT_BOTTOM_CORNER;
		break;
	}

	// 使用Ant Design风格的颜色方案获取组件颜色
	return GetComponentColors(component, RegionHighlight(region), _colors, tab);
}

// 导出装饰器插件的实例
extern "C" DecorAddOn *(instantiate_decor_addon)(image_id id, const char *name)
{
	return new (std::nothrow) AntDecorAddOn(id, name);
}
